import regeneratorRuntime from '../../../utils/regeneratorRuntime.js'

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {
      id:0,
      default: 0
    },
    region: ['陕西省', '西安市', ''],
    customItem: '全部',
    loading: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order_id = options.order_id;
    this.setData({
      order_id: order_id
    });
    var id = options.id;
    if (id) {
      this.getData(id);
    }
  },

  async getData (id) {
    try {
      const data = await app.address.getUserAddress(id)
      wx.setStorageSync('siteabout', data.about)
      this.setData({ address: data })
    } catch (err) {
      app.toast(err)
    }
  },

  del() {
    let id = this.data.address.id;
    wx.showModal({
      content: '确认要删除该地址吗？',
      confirmColor: '#FC4E11',
      success(res) {
        if (res.confirm) {
        app.delAddress(id).then(() => {
            wx.showToast({ title: '删除成功' })
            setTimeout(() => {
              wx.navigateBack()
            },1000)
          }).catch((err) => {
            app.toast('删除失败');
          });
        }
      }
    })
  },

  submit: function () {
    let address = this.data.address;
    if (address.realname == '') {
      app.toast('请输入姓名');
      return false;
    }
    if (address.mobile == '') {
      app.toast('请输入电话');
      return false;
    }

    if (!(/^1[3456789]\d{9}$/.test(address.mobile))) {
      app.toast("手机号码有误，请重填");
      return false;
    } 
    
    if (address.province == '') {
      app.toast('请选择所在地区');
      return false;
    }

    if (address.address == '') {
      app.toast('请输入详细地址');
      return false;
    }
    
    this.setData({ loading: 1 });

    var delta = 0;
    if (this.data.order_id) {
      delta = 1;
    }
    app.address.addAaddress(address).then((data) => {
      this.setData({ loading: 0 });
      if (this.data.order_id) {
        wx.setStorageSync('curr_address', data);
      }
      wx.showToast({ title: '保存成功' })
      setTimeout(()=>{
        wx.navigateBack({ delta })
      },1000)
    }).catch((err) => {
      this.setData({ loading: 0 });
      app.toast('保存失败');
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindRegionChange: function (e) {
    this.setData({
      "address.province": e.detail.value[0],
      "address.city": e.detail.value[1],
      "address.distinct": e.detail.value[2]
    });
  },
  onRealNameChange: function (e) {
    this.setData({ "address.realname": e.detail.value });
  },
  onAddressChange: function (e) {
    this.setData({ "address.address": e.detail.value });
  },
  onMobileChange: function (e) {
    this.setData({ "address.mobile": e.detail.value });
  },
  onDefaultChange: function (e) {
    this.setData({ "address.default": e.detail });
  }
})